/**
 * Created by zzy on 2017/8/21.
 */
import {observable, action, computed, observe} from 'mobx'

class WaterTiketPolicy{
    constructor(buyAmount,giveAmount,des){
        this._buyAmount=buyAmount;
        this._giveAmount=giveAmount;
        this._des=des;
    }
    _price=10;
    @observable _multiple=1;
    @observable _add2Cart=false;

    @action addMultiple(){
        this._multiple++;
    }
    @action decreaseMultiple(){
            this._multiple--
    }
    @action add2Cart(){
        this._add2Cart=true;
    }
    @action remove2Cart(){
        this._add2Cart=false;
        this._multiple=1;
    }
    @computed get des(){
        return this._des;
    }
    @computed get multiple(){
        return this._multiple;
    }

    @computed get SingelPrice(){
        return this._buyAmount*this._price*this.multiple;
    }
    @computed get hasInCart(){
        return this._add2Cart;
    }
}
class WaterTiket{
    @observable _activePolicyIndex=0;
    constructor(tiketCart){
        let t1=new WaterTiketPolicy(10,1,'买10赠1');
        let t2=new WaterTiketPolicy(20,3,'买20赠3');
        let t3=new WaterTiketPolicy(100,20,'买100赠20');
        this._policy=[t1,t2,t3];
        this._tiketCart=tiketCart;
    }
    @observable _policy=[];
    @computed get TiketPolicy(){
        return this._policy;
    }
    @computed get activePolicy(){
        return this._policy[this._activePolicyIndex]
    }
    @computed get getPolicyTotlePrice(){
        return this._policy[this._activePolicyIndex]._buyAmount*this._policy[this._activePolicyIndex]._price*this._policy[this._activePolicyIndex]._multiple
    }
    @action setActivePolicyIndex(index){
        this._activePolicyIndex=index;
    }
    @computed get activePolicyIndex(){
        return this._activePolicyIndex;
    }
    @computed get PolicyMultiple(){
        return this._policy[this._activePolicyIndex].multiple;
    }
    addMultiple(tiket){
        this._tiketCart.addMultiple(tiket);
    }
    decreaseMultiple(tiket){
        this._tiketCart.decreaseMultiple(tiket);
    }
    @action addPolicy2Cart(){
        this._policy[this._activePolicyIndex].add2Cart();
        this._tiketCart.addTiket(this._policy[this._activePolicyIndex])
    }

    @computed get hasInCart(){
        return this._policy[this._activePolicyIndex]._add2Cart
    }

    @computed get CartNum(){
        return this._tiketCart.cartNum;
    }
    @computed get CartTotlePrice(){
        return this._tiketCart.cartTotlePrice
    }
    @computed get CartTiket(){
        return this._tiketCart.getTiketCart;
    }
    CartClear(){
        this._tiketCart.clear()
    }

}
class WaterTiketCartMsg{
    @observable  _tiket=[];

    @action addTiket(tiket){
        this._tiket.push(tiket);
    }
    @action clear(){
        for(var i=0;i<this._tiket.length;i++){
            this._tiket[i].remove2Cart();
        }
        this._tiket.clear();
    }
    @action decreaseMultiple(tiket) {
        if(tiket.multiple>1){
            tiket.decreaseMultiple();
        }else {
            tiket.remove2Cart();
            this._tiket.remove(tiket);
        }

    }
    @action addMultiple(tiket){
        tiket.addMultiple();
    }

    @computed get getTiketCart(){
        return this._tiket;
    }
    @computed get getPolicy(){
        return this._policy;
    }

    @computed get cartNum(){
        return this._tiket.length;
    }
    @computed get cartTotlePrice(){
        let totlePrice=0;
            if(this._tiket.length==0){
                return 0;
            }
            for(var i=0;i<this._tiket.length;i++){
                totlePrice=totlePrice+this._tiket[i]._price*this._tiket[i]._buyAmount*this._tiket[i].multiple;
            }
        return totlePrice;
    }

}
let waterTiketCartMsg=new WaterTiketCartMsg();
let waterTiket = new WaterTiket(waterTiketCartMsg);
export default waterTiket;